PHP Results on multiple pages
PHP Results on multiple pages
am 26.01.2009 04:20:14 von Tariq Ismail Dalvi
Hello guys,
I am having a search script which supose to take results out of my mysql
database and display on a page 10 results
if the results are more then 10 it should create another page
or pages with Next >> and Prev << link so anyone can move
forward and backward on those results page.
This script do everything fine counting total records
it display's 1 to 10 records on first page with numbers but
second location where i want it to display Showing results 1 to 10 it shows
17 to 26 second it displays a link to Next but not working.
// begin to show results set
echo " found $numrows results
";
$count = 1 + $s;
// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$title = $row["sname"];
$url = $row["url"];
$message = $row["massage"];
//
$row['massage']
'\
echo "$count.) ";
echo "";
echo "
";
echo "$message
";
$count++;
}
$currPage = (($s/$limit) + 1);
//break before paging
echo "
";
// next we need to do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print " <<<
Prev 10  ";
}
// calculate number of pages needing links
// $s=intval($numrows/$limit);
$s=ceil($numrows/$limit);
// $s now contains int of pages needed unless there is a remainder from
division
if ($numrows%$limit) {
// has remainder so add one page
$s++;
}
// check to see if last page
if (!((($s+$limit)/$limit)==$s) && $s!=1) {
// not last page so give NEXT link $PHP_SELF >>
$news="$s+$limit";
echo " Next 10 >>>
";
}
$a = $s+$limit;
if ($a > $numrows) { $a = $numrows; }
$b = $s+1;
echo "Showing results $b to $a of $numrows
";
include ('footer.inc');
?>
I shall highly appreciate if anyone shall have a look at it
and let me know where i am wrong.
Thank you.
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: PHP Results on multiple pages
am 26.01.2009 22:58:01 von dmagick
> This script do everything fine counting total records
> it display's 1 to 10 records on first page with numbers but
> second location where i want it to display Showing results 1 to 10 it shows
> 17 to 26 second it displays a link to Next but not working.
It takes roughly 20 secs to do a search? Perhaps that should also be
looked at.
Try to narrow down your problem.
> // begin to show results set
> echo " found $numrows results
";
> $count = 1 + $s;
Where does $s come from? What is it supposed to mean? (Name your
variables properly).
> // now you can display the results returned
> while ($row= mysql_fetch_array($result)) {
>
> $title = $row["sname"];
> $url = $row["url"];
> $message = $row["massage"];
> //
> $row['massage']
'\
>
> echo "$count.) ";
> echo "";
> echo "
";
> echo "$message
";
> $count++;
>
> }
>
> $currPage = (($s/$limit) + 1);
>
> //break before paging
> echo "
";
>
> // next we need to do the links to other results
> if ($s>=1) { // bypass PREV link if s is 0
> $prevs=($s-$limit);
> print " <<<
> Prev 10  ";
> }
>
> // calculate number of pages needing links
> // $s=intval($numrows/$limit);
> $s=ceil($numrows/$limit);
>
>
> // $s now contains int of pages needed unless there is a remainder from
> division
>
> if ($numrows%$limit) {
> // has remainder so add one page
> $s++;
> }
What is $s set to here when there's extra pages to display?
> // check to see if last page
> if (!((($s+$limit)/$limit)==$s) && $s!=1) {
That's just too complicated to read. The first part seems redundant
since you're calculating $s anyway, why re-do it?
// are there extra pages to display?
if ($s > 1) {
echo "show extra pages";
}
> // not last page so give NEXT link $PHP_SELF >>
> $news="$s+$limit";
>
> echo " Next 10 >>>
> ";
>
> }
>
> $a = $s+$limit;
> if ($a > $numrows) { $a = $numrows; }
> $b = $s+1;
> echo "Showing results $b to $a of $numrows
";
> include ('footer.inc');
> ?>
>
> I shall highly appreciate if anyone shall have a look at it
> and let me know where i am wrong.
>
> Thank you.
>
>
--
Postgresql & php tutorials
http://www.designmagick.com/
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: PHP Results on multiple pages
am 29.01.2009 05:45:03 von Tariq Ismail Dalvi
Hello Chris,
I am inserting complete script for you to have a look at and was using
$s = $pages
// Get the search variable from URL
$var = @$_GET['q'] ;
$trimmed = trim($var); //trim whitespace from the stored variable
// rows to return
$limit=10;
// check for an empty string and display a message.
if ($trimmed == "")
{
echo "
Please enter a search...
";
exit;
}
// check for a search parameter
if (!isset($var))
{
echo "We dont seem to have a search parameter!
";
exit;
}
//connect to your database **
mysql_connect("localhost","username","password"); //(host, username,
password)
//specify database **
mysql_select_db("_database") or die("Unable to select database"); //select
which database we're using
// Build SQL Query
$query = "select * from mytable where massage like '%".$trimmed."%' order by
id";
// EDIT HERE and specify your table and field names for the SQL query
$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);
// If we have no results, offer a google search as an alternative
if ($numrows == 0)
{
echo "Results
";
echo "Sorry, your search: "" . $trimmed . "" returned zero
results
";
}
// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}
// get results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");
// display what the person searched for
echo "You searched for : "" . $var .
""";
// begin to show results set
echo " found $numrows results
";
$count = 1 + $s;
// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$title = $row["sname"];
$url = $row["url"];
$message = $row["massage"];
//
$row['massage']
'\
echo "$count.) ";
echo "";
echo "
";
echo "$message
";
$count++;
}
$currPage = (($s/$limit) + 1);
//break before paging
echo "
";
// next we need to do the links to other results <<
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print " <<<
Prev 10  ";
}
// calculate number of pages needing links
// $s=intval($numrows/$limit);
$s=ceil($numrows/$limit);
// $s now contains int of pages needed unless there is a remainder from
division
if ($numrows%$limit) {
// has remainder so add one page
$s++;
}
// check to see if last page
if (!((($s+$limit)/$limit)==$s) && $s!=1) {
// not last page so give NEXT link $PHP_SELF >>
$news="$s+$limit";
echo " Next 10 >>>
";
}
$a = $s+$limit;
if ($a > $numrows) { $a = $numrows; }
$b = $s+1;
echo "Showing results $b to $a of $numrows
";
include ('footer.inc');
?>
"Chris" wrote in message
news:497E31E9.2040808@gmail.com...
>
>> This script do everything fine counting total records
>> it display's 1 to 10 records on first page with numbers but
>> second location where i want it to display Showing results 1 to 10 it
>> shows
>> 17 to 26 second it displays a link to Next but not working.
>
> It takes roughly 20 secs to do a search? Perhaps that should also be
> looked at.
>
> Try to narrow down your problem.
>
>> // begin to show results set
>> echo " found $numrows results ";
>> $count = 1 + $s;
>
> Where does $s come from? What is it supposed to mean? (Name your variables
> properly).
>
>> // now you can display the results returned
>> while ($row= mysql_fetch_array($result)) {
>>
>> $title = $row["sname"];
>> $url = $row["url"];
>> $message = $row["massage"];
>> //
>> $row['massage']
'\
>>
>> echo "$count.) ";
>> echo "";
>> echo "
";
>> echo "$message
";
>> $count++;
>>
>> }
>>
>> $currPage = (($s/$limit) + 1);
>>
>> //break before paging
>> echo "
";
>>
>> // next we need to do the links to other results
>> if ($s>=1) { // bypass PREV link if s is 0
>> $prevs=($s-$limit);
>> print " <<<
>> Prev 10  ";
>> }
>>
>> // calculate number of pages needing links
>> // $s=intval($numrows/$limit);
>> $s=ceil($numrows/$limit);
>>
>>
>> // $s now contains int of pages needed unless there is a remainder from
>> division
>>
>> if ($numrows%$limit) {
>> // has remainder so add one page
>> $s++;
>> }
>
> What is $s set to here when there's extra pages to display?
>
>> // check to see if last page
>> if (!((($s+$limit)/$limit)==$s) && $s!=1) {
>
> That's just too complicated to read. The first part seems redundant since
> you're calculating $s anyway, why re-do it?
>
> // are there extra pages to display?
> if ($s > 1) {
> echo "show extra pages";
> }
>
>
>> // not last page so give NEXT link $PHP_SELF >>
>> $news="$s+$limit";
>>
>> echo " Next 10 >>>
>> ";
>>
>> }
>>
>> $a = $s+$limit;
>> if ($a > $numrows) { $a = $numrows; }
>> $b = $s+1;
>> echo "Showing results $b to $a of $numrows
";
>> include ('footer.inc');
>> ?>
>>
>> I shall highly appreciate if anyone shall have a look at it
>> and let me know where i am wrong.
>>
>> Thank you.
>>
>>
>
>
> --
> Postgresql & php tutorials
> http://www.designmagick.com/
>
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: PHP Results on multiple pages
am 29.01.2009 06:24:06 von dmagick
Tariq Ismail Dalvi wrote:
> Hello Chris,
>
> I am inserting complete script for you to have a look at and was using
> $s = $pages
>
> // Get the search variable from URL
>
> $var = @$_GET['q'] ;
> $trimmed = trim($var); //trim whitespace from the stored variable
>
> // rows to return
> $limit=10;
>
>
> // check for an empty string and display a message.
>
> if ($trimmed == "")
> {
> echo "
Please enter a search...
";
> exit;
> }
>
> // check for a search parameter
> if (!isset($var))
> {
> echo "We dont seem to have a search parameter!
";
> exit;
> }
I'd change that to:
if (!isset($_GET['q'])) {
echo "Search for something - or some other error message";
exit;
}
$search_term = trim($_GET['q']);
> // Build SQL Query
> $query = "select * from mytable where massage like '%".$trimmed."%'
> order by
> id";
You have sql injection here. You need to use mysql_real_escape_string:
$query = "select * from table where message like '%" .
mysql_real_escape_string($search_term) . "%'";
>
> $numresults=mysql_query($query);
> $numrows=mysql_num_rows($numresults);
>
>
> // If we have no results, offer a google search as an alternative
>
> if ($numrows == 0)
> {
>
> echo "Results
";
> echo "Sorry, your search: "" . $trimmed . "" returned zero
> results
";
>
> }
You have an xss injection problem here. You need to use htmlentities or
htmlspecialchars when you display user supplied input:
echo "Your search for "" . htmlspecialchars($trimmed) . ""
returned no results";
exit;
> // next determine if s has been passed to script, if not use 0
> if (empty($s)) {
> $s=0;
> }
> // get results
> $query .= " limit $s,$limit";
> $result = mysql_query($query) or die("Couldn't execute query");
You're re-running your query - this time with a limit.
The first query should either be a 'COUNT' (so it doesn't actually
retrieve all the results and return them it just does a count), or if
this is a mysql specific query (and will only ever be), possibly use
their special 'SQL_CALC_ROWS_FOUND' function (search
http://dev.mysql.com for it).
If it's a count, the first part will be something like:
$query = "select count(message_id) AS message_count from table where
message like '%" . mysql_real_escape_string($search_term) . "%'";
$results = mysql_query($query);
$row = mysql_fetch_assoc($results);
$messages_found = $row['message_count'];
Then run your actual search query with the limit so you only fetch 10
results.
> // display what the person searched for
> echo "You searched for : "" . $var .
> """;
XSS issue here again.
From here, rewrite it so it's a little easier to follow (and please use
variable names that make sense! $s and $q do not).
// work out pagination
// number_of_pages will be:
// $messages_found / $number_of_results_per_page
$number_of_pages = $messages_found / $number_of_results_per_page;
$current_page = 0;
if (isset($_GET['page'])) {
$current_page = (int)$_GET['page'];
}
// if we're on page 0, don't show a prev link
// if it's less than 0, someone is trying to be nasty!
if ($current_page > 0) {
echo '
}
--
Postgresql & php tutorials
http://www.designmagick.com/
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: PHP Results on multiple pages
am 03.02.2009 15:40:50 von Tariq Ismail Dalvi
Hello Guys,
Thank you for your help and suggestions the search script with pagination is
working now i may add bells and rebins to it later.
Thanks again to Chris you really put me on track.
Best regards
Tariq Dalvi
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php